package morning.cat.demo1.io.encode;

import java.io.UnsupportedEncodingException;

public class EncodeDemo {

	public static void main(String[] args) throws UnsupportedEncodingException {
		String str = "中国ABC";
		System.out.println(str);

		byte[] bytesUtf8 = str.getBytes("utf-8");
		System.out.println("utf-8:1个中文汉字占3个字节，1个英文字母占1个字节");
		for (byte b : bytesUtf8) {
			System.out.print(Integer.toHexString(b & 0xFF) + " ");
		}
		System.out.println();

		byte[] bytesGbk = str.getBytes("gbk");
		System.out.println("gbk:1个中文汉字占2个字节，1个英文字母占1个字节");
		for (byte b : bytesGbk) {
			System.out.print(Integer.toHexString(b & 0xFF) + " ");
		}
		System.out.println();

		byte[] bytesUtf16be = str.getBytes("utf-16be");
		System.out.println("utf-16be:1个中文汉字占2个字节，1个英文字母占2个字节");
		System.out.println("Java是双字节编码即utf-16be");
		for (byte b : bytesUtf16be) {
			System.out.print(Integer.toHexString(b & 0xFF) + " ");
		}
		System.out.println();
	}

}
